<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E4%BA%94-%E5%9F%BA%E4%BA%8Elfu%E7%9A%84%E7%83%AD%E7%82%B9key%E5%8F%91%E7%8E%B0%E6%9C%BA%E5%88%B6/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">534 字 ~3分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<blockquote>
<p>原文地址: <a href="https://developer.aliyun.com/article/278922">https://developer.aliyun.com/article/278922</a></p>
</blockquote>

<p><strong>简介：</strong> 业务中存在访问热点是在所难免的，redis也会遇到这个问题，然而如何发现热点key一直困扰着许多用户，redis4.0为我们带来了许多新特性，其中便包括基于LFU的热点key发现机制。</p>

<h1 id="前言">前言</h1>

<p>业务中存在访问热点是在所难免的，redis也会遇到这个问题，然而如何发现热点key一直困扰着许多用户，redis4.0为我们带来了许多新特性，其中便包括基于LFU的热点key发现机制。</p>

<h1 id="least-frequently-used">Least Frequently Used</h1>

<p>Least Frequently Used——简称LFU，意为最不经常使用，是redis4.0新增的一类内存逐出策略，关于内存逐出可以参考文章<a href="https://yq.aliyun.com/articles/257459">《Redis数据过期和淘汰策略详解》</a>。</p>

<p>从LFU的字面意思我们很容易联想到key的访问频率，但是4.0最初版本仅用来做内存逐出，对于访问频率并没有很好的记录，那么经过一番改造，redis于4.0.3版本开始正式支持基于LFU的热点key发现机制。</p>

<h2 id="lfu算法介绍">LFU算法介绍</h2>

<p>在redis中每个对象都有24 bits空间来记录LRU/LFU信息：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#204a87;font-weight:bold">typedef</span> <span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">redisObject</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#f57900">type</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#f57900">encoding</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#f57900">lru</span><span style="color:#000;font-weight:bold">:</span><span style="color:#000">LRU_BITS</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* LRU time (relative to global lru_clock) or
</span><span style="color:#8f5902;font-style:italic">                            * LFU data (least significant 8 bits frequency
</span><span style="color:#8f5902;font-style:italic">                            * and most significant 16 bits access time). */</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">refcount</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">ptr</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span> <span style="color:#000">robj</span><span style="color:#000;font-weight:bold">;</span></code></pre></div>
<p>当这24 bits用作LFU时，其被分为两部分：</p>

<ol>
<li>高16位用来记录访问时间（单位为分钟）</li>

<li><p>低8位用来记录访问频率，简称counter</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">       <span style="color:#0000cf;font-weight:bold">16</span> bits         <span style="color:#0000cf;font-weight:bold">8</span> bits
  +------------------+--------+
  + Last access <span style="color:#204a87">time</span> <span style="color:#000;font-weight:bold">|</span> LOG_C  <span style="color:#000;font-weight:bold">|</span>
  +------------------+--------+</code></pre></div></li>
</ol>

<h3 id="counter-基于概率的对数计数器">counter：基于概率的对数计数器</h3>

<p>这里读者可能会有疑问，8 bits最大值也就是255，只用8位来记录访问频率够用吗？对于counter，redis用了一个trick的手段，counter并不是一个简单的线性计数器，而是用基于概率的对数计数器来实现，算法如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c">  <span style="color:#000">uint8_t</span> <span style="color:#000">LFULogIncr</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">uint8_t</span> <span style="color:#000">counter</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
      <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">counter</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">255</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">255</span><span style="color:#000;font-weight:bold">;</span>
      <span style="color:#204a87;font-weight:bold">double</span> <span style="color:#000">r</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">double</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000">rand</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000">RAND_MAX</span><span style="color:#000;font-weight:bold">;</span>
      <span style="color:#204a87;font-weight:bold">double</span> <span style="color:#000">baseval</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">counter</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">LFU_INIT_VAL</span><span style="color:#000;font-weight:bold">;</span>
      <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">baseval</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">baseval</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
      <span style="color:#204a87;font-weight:bold">double</span> <span style="color:#000">p</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">1.0</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">baseval</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">server</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lfu_log_factor</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
      <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">r</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">p</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">counter</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#000;font-weight:bold">;</span>
      <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">counter</span><span style="color:#000;font-weight:bold">;</span>
  <span style="color:#000;font-weight:bold">}</span></code></pre></div>
<p>对应的概率分布计算公式为：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">counter</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">LFU_INIT_VAL</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">server</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lfu_log_factor</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span></code></pre></div>
<p>其中LFU_INIT_VAL为5，我们看下概率分布图会有一个更直观的认识，以默认server.lfu_log_factor=10为例：</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220616160204.png" width = 600 /> </div>

<p>从上图可以看到，counter越大，其增加的概率越小，8 bits也足够记录很高的访问频率，下表是不同概率因子server.lfu_log_factor与访问频率counter的对应关系：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#8f5902;font-style:italic"># +--------+------------+------------+------------+------------+------------+</span>
<span style="color:#8f5902;font-style:italic"># | factor | 100 hits   | 1000 hits  | 100K hits  | 1M hits    | 10M hits   |</span>
<span style="color:#8f5902;font-style:italic"># +--------+------------+------------+------------+------------+------------+</span>
<span style="color:#8f5902;font-style:italic"># | 0      | 104        | 255        | 255        | 255        | 255        |</span>
<span style="color:#8f5902;font-style:italic"># +--------+------------+------------+------------+------------+------------+</span>
<span style="color:#8f5902;font-style:italic"># | 1      | 18         | 49         | 255        | 255        | 255        |</span>
<span style="color:#8f5902;font-style:italic"># +--------+------------+------------+------------+------------+------------+</span>
<span style="color:#8f5902;font-style:italic"># | 10     | 10         | 18         | 142        | 255        | 255        |</span>
<span style="color:#8f5902;font-style:italic"># +--------+------------+------------+------------+------------+------------+</span>
<span style="color:#8f5902;font-style:italic"># | 100    | 8          | 11         | 49         | 143        | 255        |</span>
<span style="color:#8f5902;font-style:italic"># +--------+------------+------------+------------+------------+------------+</span></code></pre></div>
<p>也就是说，默认server.lfu_log_factor为10的情况下，8 bits的counter可以表示1百万的访问频率。</p>

<h3 id="counter的衰减因子">counter的衰减因子</h3>

<p>从上一小节的counter增长函数LFULogIncr中我们可以看到，随着key的访问量增长，counter最终都会收敛为255，这就带来一个问题，如果counter只增长不衰减就无法区分热点key。</p>

<p>为了解决这个问题，redis提供了衰减因子server.lfu_decay_time，其单位为分钟，计算方法也很简单，如果一个key长时间没有访问那么它的计数器counter就要减少，减少的值由衰减因子来控制：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">LFUDecrAndReturn</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">robj</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">o</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">ldt</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">o</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">lru</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">counter</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">o</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">lru</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#0000cf;font-weight:bold">255</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">num_periods</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">server</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lfu_decay_time</span> <span style="color:#ce5c00;font-weight:bold">?</span> <span style="color:#000">LFUTimeElapsed</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ldt</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">/</span> <span style="color:#000">server</span><span style="color:#000;font-weight:bold">.</span><span style="color:#f57900">lfu_decay_time</span> <span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">num_periods</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#000">counter</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">num_periods</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">counter</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">?</span> <span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#000">counter</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">num_periods</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">counter</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<p>默认为1的情况下也就是N分钟内没有访问，counter就要减N。</p>

<p>概率因子和衰减因子均可配置，推荐使用redis的默认值即可：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">lfu-log-factor <span style="color:#0000cf;font-weight:bold">10</span>
lfu-decay-time <span style="color:#0000cf;font-weight:bold">1</span></code></pre></div>
<h1 id="热点key发现">热点key发现</h1>

<p>介绍完LFU算法，接下来就是我们关心的热点key发现机制。</p>

<p>其核心就是在每次对key进行读写访问时，更新LFU的24 bits域代表的访问时间和counter，这样每个key就可以获得正确的LFU值：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">updateLFU</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">robj</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">val</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">unsigned</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">counter</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">LFUDecrAndReturn</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">val</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">counter</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">LFULogIncr</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">counter</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">val</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">lru</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">LFUGetTimeInMinutes</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">|</span> <span style="color:#000">counter</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<p>那么用户如何获取访问频率呢？redis提供了OBJECT FREQ子命令来获取LFU信息，但是要注意需要先把内存逐出策略设置为allkeys-lfu或者volatile-lfu，否则会返回错误：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#0000cf;font-weight:bold">127.0</span><span style="color:#0000cf;font-weight:bold">.0</span><span style="color:#0000cf;font-weight:bold">.1</span><span style="color:#ce5c00;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">config</span> <span style="color:#000">get</span> <span style="color:#000">maxmemory</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">policy</span>
<span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">maxmemory-policy</span><span style="color:#4e9a06">&#34;</span>
<span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">noeviction</span><span style="color:#4e9a06">&#34;</span>
<span style="color:#0000cf;font-weight:bold">127.0</span><span style="color:#0000cf;font-weight:bold">.0</span><span style="color:#0000cf;font-weight:bold">.1</span><span style="color:#ce5c00;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">object</span> <span style="color:#000">freq</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000006</span><span style="color:#0000cf;font-weight:bold">889</span>
<span style="color:#000;font-weight:bold">(</span><span style="color:#000">error</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">ERR</span> <span style="color:#000">An</span> <span style="color:#000">LFU</span> <span style="color:#000">maxmemory</span> <span style="color:#000">policy</span> <span style="color:#000">is</span> <span style="color:#000">not</span> <span style="color:#000">selected</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">access</span> <span style="color:#000">frequency</span> <span style="color:#000">not</span> <span style="color:#000">tracked</span><span style="color:#000;font-weight:bold">.</span> <span style="color:#000">Please</span> <span style="color:#000">note</span> <span style="color:#000">that</span> <span style="color:#000">when</span> <span style="color:#000">switching</span> <span style="color:#000">between</span> <span style="color:#000">policies</span> <span style="color:#000">at</span> <span style="color:#000">runtime</span> <span style="color:#000">LRU</span> <span style="color:#000">and</span> <span style="color:#000">LFU</span> <span style="color:#000">data</span> <span style="color:#000">will</span> <span style="color:#000">take</span> <span style="color:#000">some</span> <span style="color:#000">time</span> <span style="color:#000">to</span> <span style="color:#000">adjust</span><span style="color:#000;font-weight:bold">.</span>

<span style="color:#0000cf;font-weight:bold">127.0</span><span style="color:#0000cf;font-weight:bold">.0</span><span style="color:#0000cf;font-weight:bold">.1</span><span style="color:#ce5c00;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">config</span> <span style="color:#000">set</span> <span style="color:#000">maxmemory</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">policy</span> <span style="color:#000">allkeys</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">lfu</span>
<span style="color:#000">OK</span>
<span style="color:#0000cf;font-weight:bold">127.0</span><span style="color:#0000cf;font-weight:bold">.0</span><span style="color:#0000cf;font-weight:bold">.1</span><span style="color:#ce5c00;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">6379</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">object</span> <span style="color:#000">freq</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000006</span><span style="color:#0000cf;font-weight:bold">889</span>
<span style="color:#000;font-weight:bold">(</span><span style="color:#000">integer</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">3</span></code></pre></div>
<p>使用scan命令遍历所有key，再通过OBJECT FREQ获取访问频率并排序，即可得到热点key。为了方便用户使用，redis 4.0.3同时也提供了redis-cli的热点key发现功能，执行redis-cli时加上&ndash;hotkeys选项即可，示例如下：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c" data-lang="c"><span style="color:#a40000">$</span><span style="color:#000;font-weight:bold">.</span><span style="color:#ce5c00;font-weight:bold">/</span><span style="color:#000">redis</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">cli</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">hotkeys</span>

<span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic"> Scanning the entire keyspace to find hot keys as well as</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic"> average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">#</span><span style="color:#8f5902;font-style:italic"> per 100 SCAN commands (not usually needed).</span><span style="color:#8f5902;font-style:italic">
</span><span style="color:#8f5902;font-style:italic"></span>
<span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">00.00</span><span style="color:#ce5c00;font-weight:bold">%</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#000">Hot</span> <span style="color:#000">key</span> <span style="color:#a40000">&#39;</span><span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000002</span><span style="color:#a40000">&#39;</span> <span style="color:#000">found</span> <span style="color:#000">so</span> <span style="color:#000">far</span> <span style="color:#000">with</span> <span style="color:#000">counter</span> <span style="color:#0000cf;font-weight:bold">87</span>
<span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">00.00</span><span style="color:#ce5c00;font-weight:bold">%</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#000">Hot</span> <span style="color:#000">key</span> <span style="color:#a40000">&#39;</span><span style="color:#f57900">key</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000001</span><span style="color:#a40000">&#39;</span> <span style="color:#000">found</span> <span style="color:#000">so</span> <span style="color:#000">far</span> <span style="color:#000">with</span> <span style="color:#000">counter</span> <span style="color:#0000cf;font-weight:bold">254</span>
<span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">00.00</span><span style="color:#ce5c00;font-weight:bold">%</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#000">Hot</span> <span style="color:#000">key</span> <span style="color:#a40000">&#39;</span><span style="color:#000">mylist</span><span style="color:#a40000">&#39;</span> <span style="color:#000">found</span> <span style="color:#000">so</span> <span style="color:#000">far</span> <span style="color:#000">with</span> <span style="color:#000">counter</span> <span style="color:#0000cf;font-weight:bold">107</span>
<span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">00.00</span><span style="color:#ce5c00;font-weight:bold">%</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#000">Hot</span> <span style="color:#000">key</span> <span style="color:#a40000">&#39;</span><span style="color:#f57900">key</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000000</span><span style="color:#a40000">&#39;</span> <span style="color:#000">found</span> <span style="color:#000">so</span> <span style="color:#000">far</span> <span style="color:#000">with</span> <span style="color:#000">counter</span> <span style="color:#0000cf;font-weight:bold">254</span>
<span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">45.45</span><span style="color:#ce5c00;font-weight:bold">%</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#000">Hot</span> <span style="color:#000">key</span> <span style="color:#a40000">&#39;</span><span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000001</span><span style="color:#a40000">&#39;</span> <span style="color:#000">found</span> <span style="color:#000">so</span> <span style="color:#000">far</span> <span style="color:#000">with</span> <span style="color:#000">counter</span> <span style="color:#0000cf;font-weight:bold">87</span>
<span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">45.45</span><span style="color:#ce5c00;font-weight:bold">%</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#000">Hot</span> <span style="color:#000">key</span> <span style="color:#a40000">&#39;</span><span style="color:#f57900">key</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000002</span><span style="color:#a40000">&#39;</span> <span style="color:#000">found</span> <span style="color:#000">so</span> <span style="color:#000">far</span> <span style="color:#000">with</span> <span style="color:#000">counter</span> <span style="color:#0000cf;font-weight:bold">254</span>
<span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">45.45</span><span style="color:#ce5c00;font-weight:bold">%</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#000">Hot</span> <span style="color:#000">key</span> <span style="color:#a40000">&#39;</span><span style="color:#000">myset</span><span style="color:#a40000">&#39;</span> <span style="color:#000">found</span> <span style="color:#000">so</span> <span style="color:#000">far</span> <span style="color:#000">with</span> <span style="color:#000">counter</span> <span style="color:#0000cf;font-weight:bold">64</span>
<span style="color:#000;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">45.45</span><span style="color:#ce5c00;font-weight:bold">%</span><span style="color:#000;font-weight:bold">]</span> <span style="color:#000">Hot</span> <span style="color:#000">key</span> <span style="color:#a40000">&#39;</span><span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000000</span><span style="color:#a40000">&#39;</span> <span style="color:#000">found</span> <span style="color:#000">so</span> <span style="color:#000">far</span> <span style="color:#000">with</span> <span style="color:#000">counter</span> <span style="color:#0000cf;font-weight:bold">93</span>

<span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">summary</span> <span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">-</span>

<span style="color:#000">Sampled</span> <span style="color:#0000cf;font-weight:bold">22</span> <span style="color:#000">keys</span> <span style="color:#000">in</span> <span style="color:#000">the</span> <span style="color:#000">keyspace</span><span style="color:#ce5c00;font-weight:bold">!</span>
<span style="color:#000">hot</span> <span style="color:#000">key</span> <span style="color:#000">found</span> <span style="color:#000">with</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">254</span>    <span style="color:#f57900">keyname</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#f57900">key</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000001</span>
<span style="color:#000">hot</span> <span style="color:#000">key</span> <span style="color:#000">found</span> <span style="color:#000">with</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">254</span>    <span style="color:#f57900">keyname</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#f57900">key</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000000</span>
<span style="color:#000">hot</span> <span style="color:#000">key</span> <span style="color:#000">found</span> <span style="color:#000">with</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">254</span>    <span style="color:#f57900">keyname</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#f57900">key</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000002</span>
<span style="color:#000">hot</span> <span style="color:#000">key</span> <span style="color:#000">found</span> <span style="color:#000">with</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">107</span>    <span style="color:#f57900">keyname</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">mylist</span>
<span style="color:#000">hot</span> <span style="color:#000">key</span> <span style="color:#000">found</span> <span style="color:#000">with</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">93</span>    <span style="color:#f57900">keyname</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000000</span>
<span style="color:#000">hot</span> <span style="color:#000">key</span> <span style="color:#000">found</span> <span style="color:#000">with</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">87</span>    <span style="color:#f57900">keyname</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000002</span>
<span style="color:#000">hot</span> <span style="color:#000">key</span> <span style="color:#000">found</span> <span style="color:#000">with</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">87</span>    <span style="color:#f57900">keyname</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">000000000001</span>
<span style="color:#000">hot</span> <span style="color:#000">key</span> <span style="color:#000">found</span> <span style="color:#000">with</span> <span style="color:#f57900">counter</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">64</span>    <span style="color:#f57900">keyname</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">myset</span></code></pre></div>
<p>可以看到，排在前几位的即是热点key。</p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E4%BA%8C-psync2/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7%E5%9B%9B-rdb-aof%E6%B7%B7%E5%90%88%E6%8C%81%E4%B9%85%E5%8C%96/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>